package com.movecar.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.movecar.R;
import com.movecar.model.CloudBeansBean;
import com.movecar.utils.Alert;
import com.movecar.widget.CustomProgressDialog;

/**
 * 云豆提现 输入密码
 * 
 * @author Zhao.JQ
 * 
 */

public class CloudCoinsRechargeEditPasswordActivity extends Activity implements android.view.View.OnClickListener {
	private EditText passwordNumber;
	private CheckBox mCheckBox;
	private InputMethodManager imm;
	private Button ok, back;
	private CustomProgressDialog progressDialog = null;
	private LinearLayout password_mCheckBox;
	private CloudBeansBean mCloudBeansBean;
	private boolean mbDisplayFlg = false;
	private String cashCode;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recharge_check_password);
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		cashCode = getIntent().getStringExtra("cashCode");
		initView();
	}

	private void initView() {

		back = (Button) findViewById(R.id.btn_left);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		passwordNumber = (EditText) findViewById(R.id.passwordNumber);
		password_mCheckBox = (LinearLayout) findViewById(R.id.password_mCheckBox);
		password_mCheckBox.setOnClickListener(this);
		mCheckBox = (CheckBox) findViewById(R.id.mCheckBox);
		mCheckBox.setOnClickListener(this);
		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("密码验证");
		imm.showSoftInput(passwordNumber, InputMethodManager.RESULT_SHOWN);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			imm.hideSoftInputFromWindow(passwordNumber.getWindowToken(), 0);
			finish();
			break;
		case R.id.ok:

			if (!"".equals(passwordNumber.getText().toString().trim())) {
				if (passwordNumber.getText().toString().trim().length() == 6) {

					if (passwordNumber.getText().toString().trim().equals(cashCode)) {
						Alert.info(this, "密码正确");
						imm.hideSoftInputFromWindow(passwordNumber.getWindowToken(), 0);
						setResult(RESULT_OK);
						finish();
					} else {
						Alert.error(this, "密码不正确请重新输入");
					}

				} else {
					Alert.error(this, "请输入6位密码");
				}
			} else {
				Alert.error(this, "请输入密码");
			}

			break;
		case R.id.password_mCheckBox:
			mCheckBox.setChecked(!mbDisplayFlg);
			if (!mbDisplayFlg) {
				// display password text, for example "123456"
				passwordNumber.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			} else {
				// hide password, display "."
				passwordNumber.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
			mbDisplayFlg = !mbDisplayFlg;
			passwordNumber.postInvalidate();
			break;
		case R.id.mCheckBox:
			mCheckBox.setChecked(!mbDisplayFlg);
			if (!mbDisplayFlg) {
				// display password text, for example "123456"
				passwordNumber.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			} else {
				// hide password, display "."
				passwordNumber.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
			mbDisplayFlg = !mbDisplayFlg;
			passwordNumber.postInvalidate();
			break;
			
		default:
			break;
		}

	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

}
